/**
* @Description: 定义全局变量
* @Param:
* @return:
* @Author: Mr.Yan
* @Date: 2019/1/2
*/

var loginName = "",
    loginLocation = "";
/**
 * 初始化
 */
$(function () {
    init();
    tableInit();
});

/**
 * 加载layui样式
 */
function init() {
    layui.use(['form', 'laydate', 'dtree'], function () {

        var form = layui.form
            , laydate = layui.laydate
            , dtree = layui.dtree
            , nowdate = getNowFormatDate();

    });
}

// 数据展示
function tableInit() {
    layui.use(['form', 'table'], function () {
        var loading = window.top.layer.load(0, {shade: false});

        var form = layui.form
            , table = layui.table;

        //展示已知数据
        table.render({
            elem: '#tb'
            , url: prefixOnline + "/list"
            , toolbar: '#toolbar'
            , cellMinWidth: 200
            , where: {
            //  设置参数
            "params": JSON.stringify({
                    "loginName": loginName,
                    "loginLocation": loginLocation,

                })
            }
            , cols:
                [[
                    {checkbox: true, fixed: 'true'}
                    , {type: 'numbers', title: '序号', fixed: 'true'}
                    , {field: 'sessionId', title: '会话编号', fixed: 'true'}
                    , {field: 'loginName', title: '登录名称', fixed: 'true'}
                    , {field: 'deptName', title: '部门名称'}
                    , {field: 'ipaddr', title: '主机'}
                    , {field: 'loginLocation', title: '登录地点'}
                    , {field: 'browser', title: '浏览器'}
                    , {field: 'os', title: '操作系统'}
                    , {
                        field: 'status', title: '会话状态',
                        templet: function (d) {
                            switch (d.status) {
                                case "on_line" :
                                    return "<button class=\"layui-btn layui-btn-sm\">在线</button>";
                                    break;
                            }

                        }
                    }
                    , {field: 'startTimestamp', title: '登录时间'}
                    , {field: 'lastAccessTime', title: '最后访问时间'}
                ]]
            , page:
                true //是否显示分页
            , limits:
                [10, 20, 30]
            , limit:
                10 //每页默认显示的数量
            , done:
                function () {
                    layTool.toplayer.close(loading);
                }
        });

        //监听行工具事件
        table.on('toolbar(tb)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data;
            switch (obj.event) {
                case 'forceLogout':
                    forceLogout(data);
                    break;
            }
        });

    })
}

/**
 * @Description:    查询事件
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/7
 */

function serach() {
    //  用户名称
    loginName = $("#loginName").val();
    loginLocation = $("#loginLocation").val();
    tableInit();
}


/**
 * @Description: 强制退出
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/27
 */

function forceLogout(data) {
    if (!isEmpty(data)) {
        var dataLength = data.length;
        if (dataLength > 0) {
            layTool.showConfirm("你确定强制退出所选在线用户吗？",function (index,layero) {
                var loading = "";
                var sessionIds = "";
                for (var i = 0; i < dataLength; i++) {
                    sessionIds += data[i].sessionId + ",";
                }
                //确认强制退出
                $.ajax({
                    url: prefixOnline + "/forceLogout",
                    data: {
                        "sessionIds" : sessionIds.substring(0, sessionIds.length - 1)
                    },
                    type: "POST",
                    beforeSend: function () {
                        loading = layTool.toplayer.load(0, {shade: false});
                    },
                    success: function (data) {
                        layTool.toplayer.close(loading);
                        var errcode = data.errcode;
                        var errmsg = data.errmsg;
                        if (errcode == "0") {
                            tableInit();
                            layTool.closeLayer(index);
                            layTool.showSucAlert(errmsg);
                        } else {
                            layTool.showAlert(errmsg);
                        }
                    },
                    error: function (e) {
                        layTool.toplayer.close(loading);
                        AjaxErro();
                    }
                })
            });
        }else {
            layTool.showMsg("只能同时选择1个或多个需要强退的在线用户，您选择了" + dataLength + "个");
        }

    }else{
        layTool.showMsg("请选择要强退的在线用户信息");
    }

}


